<template>
	<view>
		<u-list>
			<u-list-item
				v-for="(item, index) in list"
				:key="index"
			>
				<view @click="handleOpenClose(item, index)">
					<text class="item--title">{{ item.name }}</text>
				</view>
				<u-list v-if="item.isOpen">
					<u-list-item
						v-for="(child, ichild) in item.children"
						:key="ichild"
					>
						<!--text class="item--title child" @click="selectNode(item, child)">{{ child.name }}</text-->
						<radio-group class="item--title child" >
							<radio :value="child.value" :checked="child.checked" @click="selectNode(item, child)">{{ child.name }}</radio>
						</radio-group>
					</u-list-item>
				</u-list>
			</u-list-item>
		</u-list>
	</view>
</template>

<script>
	export default {
		name: 'treeItem',
		props: {
			list: {
				item: Array,
				default: () => []
			}
		},
		methods: {
			selectNode(item, child) {
				this.$emit('change', item, child)
			},
			handleOpenClose(item, index) {
				if (!item.hasOwnProperty('isOpen')) {
					item.isOpen = false
				}
				
				item.isOpen = !item.isOpen
				this.$forceUpdate()
			}
		}
	}
</script>

<style scoped>
	.item--list {
		padding-left: 50rpx;
	}
	.item--title {
		display: flex;
		align-items: center;
		font-size: 28rpx;
		border-bottom: 1rpx solid #f7f7f7;
		padding: 20rpx;
	}
	.item--title:hover {
		background-color: #f7f7f7;
	}
	.child {
		padding: 20rpx 60rpx;
	}
</style>